package service

import (
	"apitestAfterGo/dao"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
)

type taskSuiteService struct {
	GetAllTaskSuiteByPage func(dto dto.TaskSuiteInputDto) ([]domain.TaskSuite, int)
	AddTaskSuite          func(dto domain.TaskSuite) domain.TaskSuite
	GetTaskSuiteById      func(id int) domain.TaskSuite
	ModifyTaskSuite       func(dto domain.TaskSuite) domain.TaskSuite
	DeleteTaskSuiteById   func(id int) bool
}

var TaskSuiteServiceImpl taskSuiteService

func init() {
	TaskSuiteServiceImpl = taskSuiteService{
		GetAllTaskSuiteByPage: func(dto dto.TaskSuiteInputDto) ([]domain.TaskSuite, int) {
			var taskSuites []domain.TaskSuite
			dto.Current = (dto.Current - 1) * dto.PageSize
			dao.GormDb.Debug().Where("system_id = ? and task_id = ?", dto.SystemId, dto.TaskId).Limit(dto.PageSize).Offset(dto.Current).Find(&taskSuites)
			for i := range taskSuites {
				taskSuites[i].Children = TestCaseServiceImpl.GetAllTestCaseBySuiteId(taskSuites[i].SystemId, taskSuites[i].TaskId, int(taskSuites[i].ID))
			}
			var count int64
			dao.GormDb.Debug().Model(&domain.TaskSuite{}).Select("count(1)").Where("system_id = ? and task_id = ?", dto.SystemId, dto.TaskId).Count(&count)
			return taskSuites, int(count)
		},
		AddTaskSuite: func(dto domain.TaskSuite) domain.TaskSuite {
			dao.GormDb.Debug().Create(&dto)
			return dto
		},
		GetTaskSuiteById: func(id int) domain.TaskSuite {
			var suite domain.TaskSuite
			dao.GormDb.Debug().Where("id = ?", id).Find(&suite)
			return suite
		},
		ModifyTaskSuite: func(dto domain.TaskSuite) domain.TaskSuite {
			dao.GormDb.Debug().Model(&dto).Select("*").Omit("CreateBy", "CreatedAt").Updates(dto)
			return dto
		},
		DeleteTaskSuiteById: func(id int) bool {
			dao.GormDb.Debug().Where("id = ?", id).Delete(&domain.TaskSuite{})
			return true
		},
	}
}
